package com.itguigu.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itguigu.auth.mapper.SysRolleMapper;
import com.itguigu.auth.mapper.SysUserMapper;
import com.itguigu.auth.mapper.SysUserRoleMapper;
import com.itguigu.auth.service.SysRolleService;
import com.itguigu.auth.service.SysUserRoleService;
import com.itguigu.model.system.SysRole;
import com.itguigu.model.system.SysUser;
import com.itguigu.model.system.SysUserRole;
import com.itguigu.vo.system.AssginRoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 *
 */
@Service
@Slf4j
public class SysRolleServiceImpl extends ServiceImpl<SysRolleMapper, SysRole> implements SysRolleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysRolleMapper sysRolleMapper;

    @Override
    public Map<String, Object> findRoleByUserId(Long userId) {
        //查询所有的角色
        List<SysRole> allRolesList = this.list();

        //拥有的角色id
        List<SysUserRole> existUserRoleList = sysUserRoleMapper
                .selectList(new LambdaQueryWrapper<SysUserRole>()
                        .eq(SysUserRole::getUserId, userId)
                        .select(SysUserRole::getRoleId));
        List<Long> existRoleIdList = existUserRoleList
                .stream()
                .map(SysUserRole::getRoleId)
                .collect(Collectors.toList());
        log.info("找到对应角色信息 existUserRoleList : {}", existUserRoleList);

        //对角色进行分类
        List<SysRole> assginRoleList = new ArrayList<>();
        for (SysRole role : allRolesList) {
            //已分配
            if (existRoleIdList.contains(role.getId())) {
                assginRoleList.add(role);
            }
        }

        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("assginRoleList", assginRoleList);
        roleMap.put("allRolesList", allRolesList);
        return roleMap;
    }


    @Transactional
    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        // 将之前的删除
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, assginRoleVo.getUserId());
        int delete = sysUserRoleMapper.delete(queryWrapper);
        log.info("删除了delete : {} 条数据", delete);


        // 重新进行分配
        for (Long roleId : assginRoleVo.getRoleIdList()) {
            if (StringUtils.isEmpty(roleId)) continue;
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(assginRoleVo.getUserId());
            userRole.setRoleId(roleId);
            sysUserRoleMapper.insert(userRole);
        }
    }

    @Override
    public IPage<SysRole> selectPage(Page<SysRole> pageParam, LambdaQueryWrapper<SysRole> queryWrapper) {
        return sysRolleMapper.selectPage(pageParam, queryWrapper);
    }


}
